package com.itlong.cloud.controller.project;

import com.itlong.cloud.POJO.DTO.operate.*;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.operate.WwLyOperateAppInfoVO;
import com.itlong.cloud.POJO.VO.operate.WwLyOperateProjectInfoVO;
import com.itlong.cloud.POJO.VO.operate.WwOperateAppInfoVO;
import com.itlong.cloud.POJO.VO.operate.WwOperateProjectInfoVO;
import com.itlong.cloud.annotation.UnAuthAccessRequired;
import com.itlong.cloud.annotation.UnParamEncrypt;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.project.operate.IOperateWwAppServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <des>
 *     应用控制器
 * <des>
 *
 * @create Date 2021-08-18
 **/
@RestController
@RequestMapping("")
public class OperateWwAppContoller {

    @Resource
    IOperateWwAppServiceClient iOperateWwAppServiceClient;

    /**
     * <desc>
     *      获取应用列表
     * </desc>
     * @param wwOperateSelectAppDTO
     * @return: com.itlong.cloud.utils.page.Page<com.itlong.cloud.POJO.VO.operate.WwOperateAppInfoVO>
     * @author LiuYu
     * @date: 2021-08-18
     **/
    @UnAuthAccessRequired
    @UnParamEncrypt
    @RequestMapping(path = "/getAppPageList")
    public Object getAppPageList(WwOperateSelectAppDTO wwOperateSelectAppDTO) {
        MessageVO messageVO;
        try {
            Page<WwLyOperateAppInfoVO> page=iOperateWwAppServiceClient.getAppPageList(wwOperateSelectAppDTO);
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),page);
                }catch (Exception e){
            throw new DataAccessException("【】获取应用列表失败",e);
        }
           return messageVO;
        }

    /**
     * <desc>
     *     创建应用
     * </desc>
     * @param wwLyOperateCreateAppInfoDTO
     * @return: java.lang.Integer
     * @author LiuYu
     * @date: 2021-08-19
     **/
    @UnAuthAccessRequired
    @UnParamEncrypt
    @RequestMapping(path = "/saveAppInfo")
    public Object save(WwLyOperateCreateAppInfoDTO wwLyOperateCreateAppInfoDTO) {
        MessageVO messageVO;
        try {
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iOperateWwAppServiceClient.save(wwLyOperateCreateAppInfoDTO));
            return messageVO;
        }catch (Exception e){
            throw new DataAccessException("【】创建应用失败",e);
        }
    }

    /**
     * <desc>
     *     修改应用
     * </desc>
     * @param wwLyOperateUpdateAppInfoDTO
     * @return: java.lang.Integer
     * @author LiuYu
     * @date: 2021-08-19
     **/
    @UnAuthAccessRequired
    @UnParamEncrypt
    @RequestMapping(path = "/updateAppInfo")
    public Object update(WwLyOperateUpdateAppInfoDTO wwLyOperateUpdateAppInfoDTO) {
        MessageVO messageVO;
        try {
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iOperateWwAppServiceClient.update(wwLyOperateUpdateAppInfoDTO));
            return messageVO;
        }catch (Exception e){
            throw new DataAccessException("【】修改应用失败",e);
        }
    }

    /**
    * <desc>
    *      添加开发者账号
    * </desc>
    * @param wwLyCreateDeveloperAccountDTO
    * @return: java.lang.Object
    * @author LiuYu
    * @date: 2021-08-19
     **/
    @UnAuthAccessRequired
    @UnParamEncrypt
    @RequestMapping(path = "/saveDeveloper")
    public Object saveDeveloperAccount(WwLyCreateDeveloperAccountDTO wwLyCreateDeveloperAccountDTO) {
        MessageVO messageVO;
        try {
            //正则表达式
            String regexPhoneNumber = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0-9]))\\d{8}$";
            String regexEmail ="\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
            Pattern patternPhoneNumber = Pattern.compile(regexPhoneNumber);
            Pattern patternEmail = Pattern.compile(regexEmail);
            Matcher matcherPhoneNumber = patternPhoneNumber.matcher(wwLyCreateDeveloperAccountDTO.getContactPhone());
            Matcher matcherEmail = patternEmail.matcher(wwLyCreateDeveloperAccountDTO.getContactEmail());
            if(wwLyCreateDeveloperAccountDTO == null){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            //邮箱格式校验
            if(StringUtils.isBlank(wwLyCreateDeveloperAccountDTO.getContactEmail())||!matcherEmail.matches()){
                return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
            }
            //手机号是11位
            if(wwLyCreateDeveloperAccountDTO.getContactPhone().length()!=11 || StringUtils.isBlank(wwLyCreateDeveloperAccountDTO.getContactPhone())
                    || !matcherPhoneNumber.matches()){
                return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
            }
            //字段长度不超过50且不为空
            if(wwLyCreateDeveloperAccountDTO.getCompany().length()>50 || StringUtils.isBlank(wwLyCreateDeveloperAccountDTO.getCompany())||
               wwLyCreateDeveloperAccountDTO.getContactEmail().length()>50 || StringUtils.isBlank(wwLyCreateDeveloperAccountDTO.getContactEmail())||
               wwLyCreateDeveloperAccountDTO.getContactName().length()>50|| StringUtils.isBlank(wwLyCreateDeveloperAccountDTO.getContactName())||
               wwLyCreateDeveloperAccountDTO.getCorpName().length()>50|| StringUtils.isBlank(wwLyCreateDeveloperAccountDTO.getCorpName())||
               wwLyCreateDeveloperAccountDTO.getDescription().length()>50|| StringUtils.isBlank(wwLyCreateDeveloperAccountDTO.getDescription()) ||
               wwLyCreateDeveloperAccountDTO.getDeveloperAccount().length()>50|| StringUtils.isBlank(wwLyCreateDeveloperAccountDTO.getDeveloperAccount())||
               wwLyCreateDeveloperAccountDTO.getLicenseNum().length()>50||StringUtils.isBlank(wwLyCreateDeveloperAccountDTO.getLicenseNum())||
               wwLyCreateDeveloperAccountDTO.getUsername().length()>50||StringUtils.isBlank(wwLyCreateDeveloperAccountDTO.getUsername())){
                return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
            }
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iOperateWwAppServiceClient.saveDeveloperAccount(wwLyCreateDeveloperAccountDTO));
            return messageVO;
        }catch (Exception e){
            throw new DataAccessException("【】添加开发者账号失败",e);
        }
    }

    /**
    * <desc>
    *      安全设置
    * </desc>
    * @param ipAddresses
    * @param appId
    * @return: java.lang.Object
    * @author LiuYu
    * @date: 2021-08-23
     **/
    @UnAuthAccessRequired
    @UnParamEncrypt
    @RequestMapping(path = "/setSecurity")
    public Object updateSecuritySetting(String ipAddresses, String appId){
        MessageVO messageVO;
        try {
            if(StringUtils.isBlank(ipAddresses) || StringUtils.isBlank(appId)){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iOperateWwAppServiceClient.updateSecuritySetting(ipAddresses,appId));
        }catch (Exception e){
            throw new DataAccessException("【】安全设置失败",e);
        }
        return messageVO;
    }

    /**
    * <desc>
    *   启用禁用
    * </desc>
    * @param status
    * @param appId
    * @return: java.lang.Object
    * @author LiuYu
    * @date: 2021-08-23
     **/
    @UnAuthAccessRequired
    @UnParamEncrypt
    @RequestMapping(path = "/setStatus")
    public Object updateStatus(Integer status, String appId) {
        MessageVO messageVO;
        try {
            if(status==null || StringUtils.isBlank(appId)){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iOperateWwAppServiceClient.updateStatus(status,appId));
        }catch (Exception e){
            throw new DataAccessException("【】启用禁用设置失败",e);
        }
        return messageVO;
    }

    /**
    * <desc>
    *   有效期授权设置
    * </desc>
    * @param wwLyOperateUpdateEffectTimeDTO
    * @return: java.lang.Object
    * @author LiuYu
    * @date: 2021-08-23
     **/
    @UnAuthAccessRequired
    @UnParamEncrypt
    @RequestMapping(path = "/updateEffectTime")
    public Object  updateEffectTime(WwLyOperateUpdateEffectTimeDTO wwLyOperateUpdateEffectTimeDTO) {
        MessageVO messageVO;
        try {
            if(wwLyOperateUpdateEffectTimeDTO==null || StringUtils.isBlank(wwLyOperateUpdateEffectTimeDTO.getAppProjectId())||
                    wwLyOperateUpdateEffectTimeDTO.getEffectStartTime() == null || wwLyOperateUpdateEffectTimeDTO.getEffectEndTime() == null||
                    wwLyOperateUpdateEffectTimeDTO.getAuthValidityPeriodSettype() == null){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iOperateWwAppServiceClient.updateEffectTime(wwLyOperateUpdateEffectTimeDTO));
        }catch (Exception e){
            throw new DataAccessException("【】有效期授权设置失败",e);
        }
        return messageVO;
    }
//    /**
//    * <desc>
//    *    上传图片
//    * </desc>
//    * @param file
//    * @return: java.lang.Object
//    * @author LiuYu
//    * @date: 2021-08-19
//     **/
//    @UnAuthAccessRequired
//    @UnParamEncrypt
//    @RequestMapping(path = "/uploadImage")
//    public Object uploadImage(MultipartFile file){
//        MessageVO messageVO;
//        try {
//            if(file.isEmpty()){
//                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
//            }
//            messageVO= new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iOperateWwAppServiceClient.uploadImage(file));
//            return messageVO;
//        }catch (Exception e){
//            throw new DataAccessException("【】上传图片失败",e);
//        }
//
//    }
    /**
    * <desc>
    *      查询开发者账号列表
    * </desc>
    * @param wwOperateSelectDeveloperAccountDTO
    * @return: java.lang.Object
    * @author LiuYu
    * @date: 2021-08-19
     **/
    @UnAuthAccessRequired
    @UnParamEncrypt
    @RequestMapping(path = "/getDeveloperAccountPageList")
    public Object getDeveloperAccountPageList(WwOperateSelectDeveloperAccountDTO wwOperateSelectDeveloperAccountDTO) {
        MessageVO messageVO;
        try {
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iOperateWwAppServiceClient.getDeveloperAccountPageList(wwOperateSelectDeveloperAccountDTO));
            return messageVO;
        }catch (Exception e){
            throw new DataAccessException("【】查询开发者账号列表失败",e);
        }
    }

//    /**
//    * <desc>
//    *      查询项目列表
//    * </desc>
//    * @param wwOperateSelectProjectDTO
//    * @return: java.lang.Object
//    * @author LiuYu
//    * @date: 2021-08-19
//     **/
//    @UnAuthAccessRequired
//    @UnParamEncrypt
//    @RequestMapping(path = "/getProjectPageList")
//    public Object getProjectPageList(WwOperateSelectProjectDTO wwOperateSelectProjectDTO){
//        MessageVO messageVO;
//        try {
//            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iOperateWwAppServiceClient.getProjectPageList(wwOperateSelectProjectDTO));
//            return messageVO;
//        }catch (Exception e){
//            throw new DataAccessException("【】查询项目列表失败",e);
//        }
//    }
}
